package top.hmtools.controllers;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import top.hmtools.base.StringTools;
import top.hmtools.base.ValidationTools;
import top.hmtools.beans.MapperXmlSqlPartGroupBean;
import top.hmtools.callBacks.SqlPartGroupListCallback;
import top.hmtools.commons.AlertUtil;
import top.hmtools.model.ResultModel;
import top.hmtools.random.RandomStringTools;

public class SqlPartGroupMainPaneController extends BaseController{
	@FXML
	private AnchorPane LeftAnchorPane;
	@FXML
	private TreeView<String> tv_sqlPartGroups;
	@FXML
	private AnchorPane RightAnchorPane;
	@FXML
	private Button rtb_reset;
	@FXML
	private Button rtb_update;
	@FXML
	private Button rtb_add;
	@FXML
	private Button btn_del;
	@FXML
	private TextField tf_groupName;
	@FXML
	private TextField tf_factorName;
	@FXML
	private TextArea ta_comment;
	
	/**
	 * 分组id
	 */
	@FXML
	private TextField tf_groupId;
	

	/**
	 * 重置
	 * @param event
	 */
	@FXML
	public void doReSet(ActionEvent event) {
		this.tf_factorName.setText(null);
		this.tf_groupName.setText(null);
		this.ta_comment.setText(null);
		AlertUtil.showInfoAlert("重置成功");
	}
	
	/**
	 * 执行修改
	 * @param event
	 */
	@FXML
	public void doUpdate(ActionEvent event) {
		if(StringTools.isBlank(this.tf_groupId.getText())){
			AlertUtil.showWarnAlert("请选择一条需要修改的记录");
			return;
		}
		if(!checkInput()){
			return;
		}
		
		MapperXmlSqlPartGroupBean bean = new MapperXmlSqlPartGroupBean();
		bean.setId(this.tf_groupId.getText());
		bean.setComment(this.ta_comment.getText());
		bean.setFactor_name(this.tf_factorName.getText());
		bean.setName(this.tf_groupName.getText());
		ResultModel result = this.sqlPartManageService.updateSqlPartGroup(bean);
		if(result.isStatusSuccess()){
			AlertUtil.showInfoAlert(result.getMessage());
		}else{
			AlertUtil.showErrorAlert(result.getMessage());
		}
		
		this.loadSqlPartGroupsTree();
	}
	
	@FXML
	public void doRefresh(ActionEvent event){
	    this.loadSqlPartGroupsTree();
	}
	
	@FXML
	public  void doDel(ActionEvent event){
	    if(StringTools.isBlank(this.tf_groupId.getText())){
            AlertUtil.showWarnAlert("请选择一条需要修改的记录");
            return;
        }
	    ResultModel result = this.sqlPartManageService.delSqlPartGroupById(this.tf_groupId.getText());
	    if(result.isStatusSuccess()){
            AlertUtil.showInfoAlert(result.getMessage());
        }else{
            AlertUtil.showErrorAlert(result.getMessage());
        }
	    this.loadSqlPartGroupsTree();
	}
	
	/**
	 * 新增
	 * @param event
	 */
	@FXML
	public void doAdd(ActionEvent event) {
		if(!checkInput()){
			return;
		}
		
		MapperXmlSqlPartGroupBean bean = new MapperXmlSqlPartGroupBean();
		bean.setId(RandomStringTools.getYyyyMMddHHmmssSSSPrifixInteger(6));
		bean.setComment(this.ta_comment.getText());
		bean.setFactor_name(this.tf_factorName.getText());
		bean.setName(this.tf_groupName.getText());
		ResultModel result = this.sqlPartManageService.addSqlPartGroup(bean);
		if(result.isStatusSuccess()){
			AlertUtil.showInfoAlert(result.getMessage());
		}else{
			AlertUtil.showErrorAlert(result.getMessage());
		}
		this.loadSqlPartGroupsTree();
	}
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		
	    this.tv_sqlPartGroups.setShowRoot(false);//不显示根节点
	    this.tv_sqlPartGroups.setRoot(new TreeItem<>());
	    Callback<TreeView<String>, TreeCell<String>> defaultCellFactory =  TextFieldTreeCell.forTreeView();
	    this.tv_sqlPartGroups.setCellFactory(new SqlPartGroupListCallback(defaultCellFactory,this.tf_groupId,this.tf_groupName,this.tf_factorName,this.ta_comment));
	    this.loadSqlPartGroupsTree();
		
	}
	
	/**
    * 方法说明：                    loadSqlPartGroupsTree
    * 输入参数说明：           
    * 输出参数说明：           void
    *
    **/
    @SuppressWarnings("unchecked")
    private void loadSqlPartGroupsTree() {
        TreeItem<String> root = this.tv_sqlPartGroups.getRoot();
        root.getChildren().clear();
        
      //获取所有SQL语句片段分组信息
        ResultModel result = this.sqlPartManageService.getAllSqlPartGroup();
        if(result.isStatusSuccess()){
            List<MapperXmlSqlPartGroupBean> rows = result.getRows();
            for(MapperXmlSqlPartGroupBean bean:rows){
                TreeItem<String> treeItem = new TreeItem<>();
                treeItem.setValue(bean.getName());
                
                ImageView icon = new ImageView("images/file.png");
                icon.setFitHeight(16);
                icon.setFitWidth(16);
                icon.setUserData(bean);
                treeItem.setGraphic(icon);
                root.getChildren().add(treeItem);
            }
        }else{
            AlertUtil.showErrorAlert(result.getMessage());
        }
    }

    /**
	 * 检测输入框的值
	 * @return
	 */
	private boolean checkInput(){
		boolean isFactorName = StringTools.isBlank(this.tf_factorName.getText());
		if(isFactorName){
			AlertUtil.showWarnAlert("因子名称不能为空");
			return false;
		}
		boolean isRightfulString = ValidationTools.isRightfulString(this.tf_factorName.getText());
		if(!isRightfulString){
		    AlertUtil.showWarnAlert("因子名称只能为英文字母与数字");
            return false;
		}
		boolean isGroupNameBlank = StringTools.isBlank(this.tf_groupName.getText());
		if(isGroupNameBlank){
			AlertUtil.showWarnAlert("分组名称不能为空");
			return false;
		}
		return true;
	}
}
